
package com.carsite.webapp.action;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.carsite.Constants;
import com.carsite.model.Department;
import com.carsite.model.ModuleMenu;
import com.carsite.model.Role;
import com.carsite.model.User;
import com.carsite.service.BranchManager;
import com.carsite.service.DepartmentManager;
import com.carsite.service.ModuleMenuManager;
import com.carsite.service.UserManager;
import com.carsite.webapp.form.ModuleMenuForm;



/**
 * Action class to handle CRUD on a ModuleMenu object
 *
 * @struts.action name="moduleMenuForm" path="/moduleMenus" scope="request"
 *  validate="false" parameter="method" input="mainMenu"
 * @struts.action name="moduleMenuForm" path="/editModuleMenu" scope="request"
 *  validate="false" parameter="method" input="list"
 * @struts.action name="moduleMenuForm" path="/saveModuleMenu" scope="request"
 *  validate="true" parameter="method" input="edit"
 * @struts.action-set-property property="cancellable" value="true"
 * @struts.action-forward name="edit" path="/WEB-INF/pages/moduleMenuForm.jsp"
 * @struts.action-forward name="list" path="/WEB-INF/pages/moduleMenuList.jsp"
 * @struts.action-forward name="moduleEdit" path="/WEB-INF/pages/moduleEdit.jsp"
 * @struts.action-forward name="moduleList" path="/WEB-INF/pages/moduleList.jsp"
 * @struts.action-forward name="moduleMenuTree" path="/WEB-INF/pages/moduleMenuTree.jsp"
 * @struts.action-forward name="search" path="/moduleMenus.html" redirect="true"
 * @struts.action-forward name="moduleMenuList" path="/WEB-INF/pages/moduleMenuList.jsp"
 * @struts.action-forward name="areaList" path="/WEB-INF/pages/areaBrownList_0.jsp"
 * @struts.action-forward name="moduleMenuList_c" path="/WEB-INF/pages/moduleMenuList_c.jsp"
 * @struts.action-forward name="areaList_c" path="/WEB-INF/pages/areaBrownList_c.jsp"
 * 
 * 
 * 
 * @struts.action-forward name="index" path="/index.html" redirect="true"
 * @struts.action-forward name="articleMenuTree" path="/WEB-INF/pages/articleMenuTree.jsp"
 * @struts.action-forward name="indexMenuTree" path="/WEB-INF/pages/indexMenuTree.jsp"
 * @struts.action-forward  name="index_page"  path="/front/front_index_page.jsp"
 * @struts.action-forward  name="articleList" path="/front/front_articleList.jsp"
 * @struts.action-forward name="mainMenu" path="/WEB-INF/pages/mainMenu.jsp"
 * @struts.action-forward name="mainMenuX" path="mainMenu.html" redirect="true"
 * 
 * @struts.action-forward name="bBSadd" path="/WEB-INF/pages/bbsMenuForm.jsp"
 * @struts.action-forward name="noLogin" path="/front/front_noLogin.jsp"
 * @struts.action-forward name="noLogin" path="/front/front_noLogin.jsp"
 * @struts.action-forward name="bbsList" path="/front/front_bbsList.jsp"
 * 
 * 
 * 
 */
public final class ModuleMenuAction extends BaseAction {
    public ActionForward cancel(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        return mapping.findForward("search");
    }
  
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'delete' method");
        }
        if(!this.check(request)){
        	return null;
        }
        ActionMessages messages = new ActionMessages();
        ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;

        // Exceptions are caught by ActionExceptionHandler
        ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
        mgr.removeModuleMenu(moduleMenuForm.getMenuId());

        messages.add(ActionMessages.GLOBAL_MESSAGE,
                     new ActionMessage("moduleMenu.deleted"));

        // save messages in session, so they'll survive the redirect
        saveMessages(request.getSession(), messages);
        
        
      
        List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        
        
        moduleMenuForm = new ModuleMenuForm();
        updateFormBean(mapping, request, moduleMenuForm);;
	    
	    return mapping.findForward("edit");
	     
    }
    
    
    /**enter menu edit page**/
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'edit' method");
        }
        if(!this.check(request)){
        	return null;
        }
        ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;

        ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
        BranchManager branchMgr = (BranchManager) getBean("branchManager");
        if (moduleMenuForm.getMenuId() != null) {
          
            ModuleMenu moduleMenu = mgr.getModuleMenu(moduleMenuForm.getMenuId());
           
            moduleMenuForm = (ModuleMenuForm) convert(moduleMenu);
            
            //if parent is null,be top menu.
            if(moduleMenu.getParent()!=null){
            	moduleMenuForm.setParentId(String.valueOf(moduleMenu.getParent().getMenuId()));
            	moduleMenuForm.setParentName(moduleMenu.getParent().getMenuName());	
            }
            
            updateFormBean(mapping, request, moduleMenuForm);
            
        }else {
         	
        	// default parent
         	if(moduleMenuForm.getParentId()!=null){
         	    ModuleMenu moduleMenu = mgr.getModuleMenu(moduleMenuForm.getParentId());
         	   moduleMenuForm.setParentId(String.valueOf(moduleMenu.getMenuId()));
         	  moduleMenuForm.setParentName(moduleMenu.getMenuName());	
             }	
         	moduleMenuForm.setPostion("-1");
         	moduleMenuForm.setTopNum("-1");
         }
        
        List moduleList = mgr.getModules();
        
        List topBranchList = branchMgr.getTopBranchs();
        request.setAttribute(Constants.BRANCH_LIST,topBranchList);
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);

        return mapping.findForward("edit");
       
    }
    public ActionForward bBSadd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'bBSadd' method");
		}
		if (!this.check(request)) {
			return null;
		}
		ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;

        ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
        if (moduleMenuForm.getMenuId() != null) {
            ModuleMenu moduleMenu = mgr.getModuleMenu(moduleMenuForm.getMenuId());
            moduleMenuForm = (ModuleMenuForm) convert(moduleMenu);
            //if parent is null,be top menu.
            if(moduleMenu.getParent()!=null){
            	moduleMenuForm.setParentId(String.valueOf(moduleMenu.getParent().getMenuId()));
            	moduleMenuForm.setParentName(moduleMenu.getParent().getMenuName());	
            }
            
            updateFormBean(mapping, request, moduleMenuForm);
            
        }else {
         	
        	// default parent
         	if(moduleMenuForm.getParentId()!=null){
         	    ModuleMenu moduleMenu = mgr.getModuleMenu(moduleMenuForm.getParentId());
         	   moduleMenuForm.setParentId(String.valueOf(moduleMenu.getMenuId()));
         	  moduleMenuForm.setParentName(moduleMenu.getMenuName());	
             }	
         	moduleMenuForm.setPostion("-1");
         	moduleMenuForm.setTopNum("-1");
         }
        
        List moduleList=new LinkedList();
        ModuleMenu mm=new ModuleMenu();
        mm.setMenuName("请选择");
        moduleList.add(mm);
        List mList= mgr.getModuleMenus(new ModuleMenu());
        for(Object obj:mList){
        	moduleList.add(obj);
        }
       
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        request.setAttribute("deptId", request.getParameter("deptId"));
        Enumeration enumeration=request.getAttributeNames();
        while(enumeration.hasMoreElements()){
        	String name=(String)enumeration.nextElement();
        	System.out.println(name);
        }
		return mapping.findForward("bBSadd");
       

	}
    /**menu save**/
    public ActionForward save(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
      
    	if (log.isDebugEnabled()) {
            log.debug("Entering 'save' method");
        }
    	
    	if(!this.check(request)){
        	return null;
        }
        ActionMessages messages = new ActionMessages();
        ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
        boolean isNew = ("".equals(moduleMenuForm.getMenuId()) || moduleMenuForm.getMenuId() == null);
        ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
        
        
        ModuleMenu moduleMenu = (ModuleMenu) convert(moduleMenuForm);
        
        
        if(isNew){
        
            if(moduleMenuForm.getParentId()!=null && !"".equals(moduleMenuForm.getParentId())){
            
            	ModuleMenu parent =  mgr.getModuleMenu(moduleMenuForm.getParentId());
            
            	moduleMenu.setParent(parent);
            
            	Long deep=parent.getDeep();
            	if(deep==null||deep==0){
            		moduleMenu.setDeep(1L);
            	}else{
            		moduleMenu.setDeep(parent.getDeep()+1);
            	}
            }
            
            mgr.saveModuleMenu(moduleMenu);
            
        }else{
        	
        	ModuleMenu dbModuleMenu = mgr.getModuleMenu(moduleMenuForm.getMenuId());
        	
        
        	dbModuleMenu.setMenuName(moduleMenuForm.getMenuName());
        	dbModuleMenu.setTarget(moduleMenuForm.getTarget());
        	dbModuleMenu.setTitle(moduleMenuForm.getTitle());
        	dbModuleMenu.setUrl(moduleMenuForm.getUrl());
        	dbModuleMenu.setOpen(moduleMenuForm.isOpen());
        	
        	dbModuleMenu.setTopNum(new Integer(moduleMenuForm.getTopNum()));
        	dbModuleMenu.setStatus(moduleMenuForm.isStatus());
        	dbModuleMenu.setPostion(new Integer(moduleMenuForm.getPostion()));
        	
        	
            if (moduleMenuForm.getParentId() != null
					&& !"".equals(moduleMenuForm.getParentId())) {

				String dbParentCateId = null;
				if (dbModuleMenu.getParent() != null) {
					dbParentCateId = String.valueOf(dbModuleMenu.getParent()
							.getMenuId());
				}

				if (!moduleMenuForm.getParentId().equals(dbParentCateId)) {
					ModuleMenu parent = mgr.getModuleMenu(moduleMenuForm
							.getParentId());
					dbModuleMenu.setParent(parent);
				}
			} else {
				dbModuleMenu.setParent(null);
			}
			
            mgr.saveModuleMenu(dbModuleMenu);
        	
        }

        // add success messages
        if (isNew) {
            messages.add(ActionMessages.GLOBAL_MESSAGE,
                         new ActionMessage("moduleMenu.added"));

            // save messages in session to survive a redirect
            saveMessages(request.getSession(), messages);

          
        } else {
            messages.add(ActionMessages.GLOBAL_MESSAGE,
                         new ActionMessage("moduleMenu.updated"));
            saveMessages(request, messages);

        }
        
        
        //return to empty edit page
        moduleMenuForm = new ModuleMenuForm();
        updateFormBean(mapping, request, moduleMenuForm);
        
        //all module
        List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        
        return mapping.findForward("edit");
       
    }
    public ActionForward saveBBS(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
throws Exception {

if (log.isDebugEnabled()) {
log.debug("Entering 'save' method");
}

if(!this.check(request)){
return null;
}
ActionMessages messages = new ActionMessages();
ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
System.out.println("menuId:"+moduleMenuForm.getMenuId());
System.out.println("deptId:"+moduleMenuForm.getDeptId());
boolean isNew = ("".equals(moduleMenuForm.getMenuId()) || moduleMenuForm.getMenuId() == null);
ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
DepartmentManager deptMgr = (DepartmentManager) getBean("departmentManager");

ModuleMenu moduleMenu = (ModuleMenu) convert(moduleMenuForm);


if(isNew){
moduleMenu.setBBS(true);
if(moduleMenuForm.getParentId()!=null && !"".equals(moduleMenuForm.getParentId())){

ModuleMenu parent =  mgr.getModuleMenu(moduleMenuForm.getParentId());

moduleMenu.setParent(parent);
Long deep=parent.getDeep();
if(deep==null||deep==0){
	moduleMenu.setDeep(1L);
}else{
	moduleMenu.setDeep(parent.getDeep()+1);
}
}
if(moduleMenuForm.getDeptId()!=null&&!"".equals(moduleMenuForm.getDeptId())){
	Department dept=deptMgr.getDepartment(moduleMenuForm.getDeptId());
	moduleMenu.setDept(dept);
	moduleMenu.setBranch(dept.getBranch());
	
}
mgr.saveModuleMenu(moduleMenu);

}else{

ModuleMenu dbModuleMenu = mgr.getModuleMenu(moduleMenuForm.getMenuId());


dbModuleMenu.setMenuName(moduleMenuForm.getMenuName());
dbModuleMenu.setTarget(moduleMenuForm.getTarget());
dbModuleMenu.setTitle(moduleMenuForm.getTitle());
dbModuleMenu.setUrl(moduleMenuForm.getUrl());
dbModuleMenu.setOpen(moduleMenuForm.isOpen());

dbModuleMenu.setTopNum(new Integer(moduleMenuForm.getTopNum()));
dbModuleMenu.setStatus(moduleMenuForm.isStatus());
dbModuleMenu.setPostion(new Integer(moduleMenuForm.getPostion()));

Department dept=deptMgr.getDepartment(moduleMenuForm.getDeptId());
dbModuleMenu.setDept(dept);
dbModuleMenu.setBranch(dept.getBranch());

if (moduleMenuForm.getParentId() != null
	&& !"".equals(moduleMenuForm.getParentId())) {

String dbParentCateId = null;
if (dbModuleMenu.getParent() != null) {
	dbParentCateId = String.valueOf(dbModuleMenu.getParent()
			.getMenuId());
}

if (!moduleMenuForm.getParentId().equals(dbParentCateId)) {
	ModuleMenu parent = mgr.getModuleMenu(moduleMenuForm
			.getParentId());
	dbModuleMenu.setParent(parent);
}
} else {
dbModuleMenu.setParent(null);
}

mgr.saveModuleMenu(dbModuleMenu);

}

// add success messages
if (isNew) {
messages.add(ActionMessages.GLOBAL_MESSAGE,
       new ActionMessage("moduleMenu.added"));

// save messages in session to survive a redirect
saveMessages(request.getSession(), messages);


} else {
messages.add(ActionMessages.GLOBAL_MESSAGE,
       new ActionMessage("moduleMenu.updated"));
saveMessages(request, messages);


}


//return to empty edit page
moduleMenuForm = new ModuleMenuForm();
updateFormBean(mapping, request, moduleMenuForm);

//all module
List moduleList = mgr.getModules();
request.setAttribute(Constants.MODULEMENU_LIST,moduleList);

return mapping.findForward("bBSadd");

}

    
    public ActionForward search(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'search' method");
        }

        ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
        ModuleMenu moduleMenu = (ModuleMenu) convert(moduleMenuForm);

        ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
        request.setAttribute(Constants.MODULEMENU_LIST, mgr.getModules());

        return mapping.findForward("list");        
        
    }
    
    
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
            throws Exception {
        return search(mapping, form, request, response);
    }
    
    
    /**
     * enter edit page
     */
    public ActionForward moduleEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
		if (log.isDebugEnabled()) {
		   log.debug("Entering 'moduleEdit' method");
		}
		if(this.check(request)){
        	return null;
        }
		ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
		
		// if an id is passed in, look up the user - otherwise
		// don't do anything - user is doing an add
		if (moduleMenuForm.getMenuId() != null) {
			ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
			ModuleMenu moduleMenu = mgr.getModuleMenu(moduleMenuForm.getMenuId());
			moduleMenuForm = (ModuleMenuForm) convert(moduleMenu);
			updateFormBean(mapping, request, moduleMenuForm);
		}
		
		return mapping.findForward("moduleEdit");
	}
    
    
    /**
     * module save
     */
    public ActionForward moduleSave(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
    	 if (log.isDebugEnabled()) {
             log.debug("Entering 'moduleSave' method");
         }

         // Extract attributes and parameters we will need
         ActionMessages messages = new ActionMessages();
         ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
         boolean isNew = ("".equals(moduleMenuForm.getMenuId()) || moduleMenuForm.getMenuId() == null);

         ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
         ModuleMenu moduleMenu = (ModuleMenu) convert(moduleMenuForm);
         mgr.saveModuleMenu(moduleMenu);

         // add success messages
         if (isNew) {
             messages.add(ActionMessages.GLOBAL_MESSAGE,
                          new ActionMessage("moduleMenu.moduleadded"));

             // save messages in session to survive a redirect
             saveMessages(request.getSession(), messages);
         } else {
             messages.add(ActionMessages.GLOBAL_MESSAGE,
                          new ActionMessage("moduleMenu.moduleupdated"));
             saveMessages(request, messages);
         }
         moduleMenuForm = (ModuleMenuForm) convert(moduleMenu);
		 updateFormBean(mapping, request, moduleMenuForm);
			
		return mapping.findForward("moduleEdit");
	}
    
    
    /**
     * module list
     */
    public ActionForward moduleList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
    	
    	if (log.isDebugEnabled()) {
             log.debug("Entering 'moduleList' method");
         }
    	HttpSession session=request.getSession();
    	//Object currentUser=session.getAttribute("ACEGI_SECURITY_LAST_USERNAME");//currentUser--是String类
    	Object currentUser=session.getAttribute("currentUser");//currentUser--是String类
    	
    	if(currentUser==null){
    		return mapping.findForward("index");
    	}
    	 String id=request.getParameter("id");
    	 if(id==null){
         ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
         ModuleMenu moduleMenu = (ModuleMenu) convert(moduleMenuForm);

         ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
         request.setAttribute(Constants.MODULEMENU_LIST, mgr.getModules());
         return mapping.findForward("moduleList");
    	 }else{
    		 ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
             ModuleMenu moduleMenu = (ModuleMenu) convert(moduleMenuForm);

             ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
          //   NewsArticleManager articlemgr = (NewsArticleManager) getBean("newsArticleManager");
          //   SysFileManager filemgr = (SysFileManager) getBean("sysFileManager");
             List menulist=mgr.getModuleMenusByParent(id);
          //   List articleList=articlemgr.getNewsArticles(id);
          //   List sysFileList=filemgr.getSysFilesByMenuId(id);
             request.setAttribute(Constants.MODULEMENU_LIST,menulist);
             //////////////////////////////////////////////////
           //  request.setAttribute("articleList", articleList);
          //   request.setAttribute("sysFileList", sysFileList);
             //////////////////////////////////////////////////
       		return mapping.findForward("moduleMenuList_c");
       		
            
    	 }
	}
    
    /**module delete**/
    public ActionForward moduleDelete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'moduleDelete' method");
		}
		
		ActionMessages messages = new ActionMessages();
		ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
		
		// Exceptions are caught by ActionExceptionHandler
		ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
		mgr.removeModuleMenu(moduleMenuForm.getMenuId());
		
		messages.add(ActionMessages.GLOBAL_MESSAGE,
		 new ActionMessage("moduleMenu.moduledeleted"));
		
		// save messages in session, so they'll survive the redirect
		saveMessages(request.getSession(), messages);
		 return moduleList(mapping, form, request, response);
		
	}
    /**enter tree view**/
    public ActionForward moduleMenuTree(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'moduleMenuTree' method");
		}
		
		ActionMessages messages = new ActionMessages();
		ModuleMenuForm moduleMenuForm = (ModuleMenuForm) form;
		
		// Exceptions are caught by ActionExceptionHandler
		ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");

        
        
        List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        
	
		return mapping.findForward("moduleMenuTree");
		  
	}
    
    public ActionForward articleMenuTree(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'moduleMenuTree' method");
		}
		// Exceptions are caught by ActionExceptionHandler
		ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");

        
        
        List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        
	
		return mapping.findForward("articleMenuTree");
		  
	}
    public ActionForward indexMenuTree(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'moduleMenuTree' method");
		}
		// Exceptions are caught by ActionExceptionHandler
		ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");

        
        
        List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        
	
		return mapping.findForward("indexMenuTree");
		  
	}
    
//  列出某一栏目下所有文章
	  
    
    public ActionForward childrenList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
	throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'moduleMenuTree' method");
		}
		// Exceptions are caught by ActionExceptionHandler
		ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");

        
        
        List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
        
	
		return mapping.findForward("articleMenuTree");
		  
	}
    public ActionForward indexPage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
		throws Exception {
		   return mapping.findForward("index_page");
		}
    public ActionForward login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'add' method");
		}
		UserManager userMgr = (UserManager) getBean("userManager");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
	
		User user=new User();
		user.setUsername(username);
		user.setPassword(password);
		
		
		User currentUser=null;
		try {
			currentUser = userMgr.login(username,password);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		///List list=null;
		if(currentUser!=null){
		HttpSession session=request.getSession(true);
		if(currentUser.isEnabled()){
		session.setAttribute("currentUser", currentUser);
		}
		Set roles=currentUser.getRoles();
		Iterator rolesIt=roles.iterator();
		while(rolesIt.hasNext()){
			Role role=(Role)rolesIt.next();
			if(role.getName().equals("commuser")){
				ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
				List moduleList = mgr.getModules();
		        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
				return mapping.findForward("indexMenuTree");
			}else{
				ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
				List moduleList = mgr.getModules();
		        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
				return mapping.findForward("indexMenuTree");//
			}
		}
		}
		
		ModuleMenuManager mgr = (ModuleMenuManager) getBean("moduleMenuManager");
		List moduleList = mgr.getModules();
        request.setAttribute(Constants.MODULEMENU_LIST,moduleList);
		return mapping.findForward("indexMenuTree");//
		

	}

	public ActionForward mainMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		if(!this.check(request,new String("menuId"))){
        	return mapping.findForward("noLogin");
        }
		return mapping.findForward("mainMenuX");
	}
	 public boolean check(HttpServletRequest request){
		 HttpSession session=request.getSession();
		 if(session==null){
			 return false;
		 }else{
			 User currentUser=(User)session.getAttribute("currentUser");
			 if(currentUser==null){
				 return false;
			 }else{
				 Set roles=currentUser.getRoles();
				 Iterator rolesIt=(Iterator)roles.iterator();
				 while(rolesIt.hasNext()){
					 Role role=(Role)rolesIt.next();
					 System.out.println(role.getName());
					 if(role.getName().equals("admin")){
						 return true;
					 }
				 }
				 return false;
			 }
		 }
		
	 }
	 public boolean check(HttpServletRequest request,String menuId){
		 HttpSession session=request.getSession();
		 if(session==null){
			 return false;
		 }else{
			 User currentUser=(User)session.getAttribute("currentUser");
			 if(currentUser==null){
				 return false;
			 }else{
				 Set roles=currentUser.getRoles();
				 Iterator rolesIt=(Iterator)roles.iterator();
				 while(rolesIt.hasNext()){
					 
					 Role role=(Role)rolesIt.next();
					 System.out.println(role.getName());
					 if(role.getName().equals("admin")){
						 return true;
					 }
					 Set menus=role.getPrivileges();
					 Iterator menusIt=(Iterator)menus.iterator();
					 while(menusIt.hasNext()){
						return true;
						
					 }
					 
				 }
				 return false;
			 }
		 }
		
	 }
	 public ActionForward logout(ActionMapping mapping, ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)
throws Exception {
request.getSession().removeAttribute("currentUser");
return mapping.findForward("index");
}
}
